<template>
	<view class="content">
		<!-- #ifdef APP-PLUS || H5 -->
		<view class="content echarts" @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts"
			id="echartsline"></view>
		<!-- #endif -->
		<!-- #ifndef APP-PLUS || H5 -->
		<view>非 APP、H5 环境不支持</view>
		<!-- #endif -->
	</view>
</template>

<script>
	import echarts from "../../static/common/js/echarts.min.js"
	export default {
		name: 'chartLine',
		props: {
			chartLineData: {
				type: Object,
				default () {
					return {}
				}
			}
		},
		data() {
			return {
				option: {
					// title: {
					// 	text: '折线堆叠图'
					// },
					tooltip: {
						trigger: 'axis'
					},
					//图例
					legend: {
						data: [], //例：['厂区', '港口', '订单']
						show: true,
						x: 'center',
						y: 'bottom',
					},
					//整个图表区域
					grid: {
						left: '3%',
						right: '3%',
						top: '5%',
						bottom: '10%',
						containLabel: true
					}, 
					//x轴
					xAxis: {
						type: 'category',
						boundaryGap: false,
						data: []
					},
					//y轴
					yAxis: {
						type: 'value',
						axisLine: {
							show: false //y轴线消失
						},
						axisTick: {
							show: false //y轴坐标点消失,
						},
					},
					//数据
					series: []
				}
			}
		},
		mounted() {
			this.handleData()
		},
		watch: {
			chartLineData: function(newQuestion, oldQuestion) {
				this.handleData()
			}
		},
		methods: {
			handleData() {
				if (this.chartLineData.legendData) {
					this.option.legend.data = this.chartLineData.legendData;
				}
				if (this.chartLineData.xAxisDataList) {
					this.option.xAxis.data = this.chartLineData.xAxisDataList;
				}
				if (this.chartLineData.yAxisDataList) {
					this.option.series = []; //清空series
					for (var i = 0; i < this.chartLineData.yAxisDataList.length; i++) {
						let item = this.chartLineData.yAxisDataList[i];
						let line;
						if(this.option.legend.data.length > i) {
							line = this.handleLine(this.option.legend.data[i], item.data, item.color);
						} else {
							line = this.handleLine('', item.data, item.color);
						}
						this.option.series.push(line);
					}
				}
			},

			//获取折线图配置
			// name: 名称
			// color: 折线颜色
			// data：折线y轴数据
			handleLine(name, data, color) {
				return {
					name: name,
					type: 'line',
					stack: 'total',
					itemStyle: {
						color: color
					},
					emphasis: {
						focus: 'series'
					},
					data: data,
					areaStyle: {
						normal: {
							color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
									offset: 1,
									color: color
								},
								{
									offset: 0,
									color: color
								}
							], false),
							shadowColor: 'rgba(108,80,243, 0.01)',
							shadowBlur: 20
						}
					}
				}
			},

			onViewClick(options) {
				console.log(options)
			}
		}
	}
</script>

<script module="echarts" lang="renderjs">
	let myChart
	export default {
		mounted() {
			if (typeof window.echarts === 'function') {
				this.initEcharts()
			} else {
				// 动态引入较大类库避免影响页面展示
				const script = document.createElement('script')
				// view 层的页面运行在 www 根目录，其相对路径相对于 www 计算
				script.src = 'static/common/js/echarts.min.js'
				script.onload = this.initEcharts.bind(this)
				document.head.appendChild(script)
			}
		},
		methods: {
			initEcharts() {
				myChart = echarts.init(document.getElementById('echartsline'))
				// 观测更新的数据在 view 层可以直接访问到
				// this.option.tooltip.formatter =function(params) {
				// 	let returnData = "";
				// 	returnData +='<view>'+params[0].name+'</view></br>'
				// 	for (let i=0;i<params.length;i++){
				// 	  if (params[i].seriesName!=""){
				// 		let indexColor = params[i].color;
				// 		returnData += '<view style="display:inline-block;margin-right:2px;border-radius:10px;width:9px;height:9px;background:'+ indexColor +'"></view>';
				// 		returnData += params[i].seriesName +": " + (params[i].value==''?0:params[i].value) + "吨</br>";
				// 	  }
				// 	}
				// 	return returnData;
				// }

				myChart.setOption(this.option)
			},
			updateEcharts(newValue, oldValue, ownerInstance, instance) {
				// 监听 service 层数据变更
				if (myChart !== undefined) {
					myChart.setOption(newValue)
				}
			},
			onClick(event, ownerInstance) {
				// 调用 service 层的方法
				ownerInstance.callMethod('onViewClick', {
					test: 'test'
				})
			}
		}
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.echarts {
		width: 100%;
		height: 500rpx;
	}
</style>
